package cn.sicnu.cs.employment.service.impl;

import cn.sicnu.cs.employment.domain.entity.*;
import cn.sicnu.cs.employment.exception.CustomException;
import cn.sicnu.cs.employment.mapper.BackgroundCommentsMapper;
import cn.sicnu.cs.employment.mapper.EmployeeCompanyMapper;
import cn.sicnu.cs.employment.mapper.EmployeeExpMapper;
import cn.sicnu.cs.employment.mapper.EmployeeInfoMapper;
import cn.sicnu.cs.employment.service.IEmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Set;

import static cn.sicnu.cs.employment.common.Constants.SAVED_ERROR;
import static cn.sicnu.cs.employment.common.util.RequestUtil.getCurrentUser;

@Service
@RequiredArgsConstructor
public class EmployeeServiceImpl implements IEmployeeService {

    private final EmployeeInfoMapper employeeInfoMapper;
    private final EmployeeExpMapper employeeExpMapper;
    private final EmployeeCompanyMapper empcomMapper;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void addUserInfo(EmployeeInfo employeeInfo, Long userId) {
        EmployeeInfo info = employeeInfo.withUserId(userId);
        if (ObjectUtils.isEmpty(employeeInfoMapper.selectById(userId))) {
            employeeInfoMapper.insert(info);
        } else {
            employeeInfoMapper.updateById(info);
        }
    }

    @Override
    public EmployeeInfo getUserInfoById(Long empId) {
        return employeeInfoMapper.selectById(empId);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public EmployeeInfo getUserInfoByUserId(Long userId) {
        return employeeInfoMapper.selectByUserId(userId);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void updateHeadImg(Long userId, String path) {
        int updated = employeeInfoMapper.updateHeadImg(userId, path);
        if (updated < 1){
            throw new CustomException(SAVED_ERROR, "保存头像失败！");
        }
    }

    @Override
    public String getHeadImg(Long id) {
        return employeeInfoMapper.selectHeadImg(id);
    }

    @Override
    public boolean isUserInfoExisted(Long userId) {
        return employeeInfoMapper.countByUserId(userId) > 0;
    }

    @Override
    public Set<EmployeeExpEducation> getEducationExp(Long id) {
        return employeeExpMapper.selectEducationExp(id);
    }

    @Override
    public Set<EmployeeExpInternship> getInternshipExp(Long id) {
        return employeeExpMapper.selectInternshipExp(id);

    }

    @Override
    public Set<EmployeeExpProject> getProjectExp(Long id) {
        return employeeExpMapper.selectProjectExp(id);

    }

    @Override
    public Long getCurrentEmpId() {
        return employeeInfoMapper.selectEmpId(getCurrentUser().getId());
    }

    @Override
    public String getEmpNameById(Long empId) {
        return employeeInfoMapper.getEmpName(empId);
    }

    @Override
    public Set<EmployeeCompany> getAllEmpComByEmpId(Long empId) {
        return empcomMapper.selectAllByEmp(empId);
    }

    @Override
    public Set<EmployeeExpProject> getProjectExp(Long id, Long comId) {
        return employeeExpMapper.selectProjectExpByComAndEmp(id,comId);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class, propagation = Propagation.REQUIRED)
    public void saveProjectExp(EmployeeExpProject bgToSave) {
        employeeExpMapper.insert(bgToSave);
    }

    @Override
    public boolean isBgAvailable(Long id) {
        long create = employeeExpMapper.getCreateTime(id).getTime();
        long now = System.currentTimeMillis();
        long timeLimit = 30 * 24 * 60 * 60 * 1000L;
        return now - create < timeLimit ;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class, propagation = Propagation.REQUIRED)
    public void deleteProjectExp(Long id) {
        employeeExpMapper.deleteById(id);
    }

    @Override
    public EmployeeExpProject getProjectExpById(Long expProId) {
        return employeeExpMapper.selectById(expProId);
    }

    @Override
    public void updateEmpInfo(EmployeeInfo build) {
        employeeInfoMapper.updateById(build);
    }

    @Override
    public Long findComIdByEmpId(Long currentEmpId) {
        return empcomMapper.selectComIdByEmp(currentEmpId);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class, propagation = Propagation.REQUIRED)
    public void addBackExpSelf(String content, Long id) {
        employeeExpMapper.updateById(
                EmployeeExpProject.builder()
                        .id(id)
                        .empId(getCurrentEmpId())
                        .self(content)
                        .build()
        );
    }
}
